package com.hursing.tractor.controller;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

import com.hursing.tractor.GameView;
import com.hursing.tractor.R;
import com.hursing.tractor.baseview.UIButton;
import com.hursing.tractor.baseview.UICardView;
import com.hursing.tractor.baseview.UIFlippedSuitView;
import com.hursing.tractor.baseview.UIHandyCardView;
import com.hursing.tractor.baseview.UIImageButton;
import com.hursing.tractor.baseview.UILabel;
import com.hursing.tractor.baseview.UITextButton;
import com.hursing.tractor.baseview.UIView;
import com.hursing.tractor.baseview.UIWindow;
import com.hursing.tractor.model.Player;
import com.hursing.tractor.model.Card;
import com.hursing.tractor.model.GameData;
import com.hursing.tractor.model.Strategy;
import com.hursing.tractor.view.UIDesktopPanelView;
import com.hursing.tractor.view.UIFlipPanelView;
import com.hursing.tractor.view.UIHelper;
import com.hursing.tractor.view.UIPlayPanelView;
import com.hursing.tractor.view.UISingleButtonPanelView;
import com.hursing.tractor.view.UIStatusPanelView;

final class ViewAssistant implements GameView.GameViewDelegate, UIButton.OnTouchDownListener {
	
	static final int MESSAGE_CANCEL_SHOW_KITTY = 3;
	static final int MESSAGE_CANCEL_SHOW_PREVIOUS = 4;
	static final int INVERVAL_CANCEL = 1000;
		
	private Handler mViewHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MESSAGE_CANCEL_SHOW_KITTY:
				UIView.sViews[UIView.VIEW_KITTY_CARD].visible(false);
				break;
			case MESSAGE_CANCEL_SHOW_PREVIOUS:
				for (int i = 0; i < Player.PLAYER_COUNT; i++) {
					UIView.sViews[UIView.VIEW_BOTTOM_CARD + i].toCardView().cards(GameData.instance().playedCards().get(i));
				}
				break;
			default:
				break;
			}
		}
	};
	
	//---------------GameViewDelegate-----------------------
	@Override
	public void onDraw(Canvas canvas) {
		UIWindow.instance().onDraw(canvas);
	}

	@Override
	public void onSizeChanged(int w, int h) {
		UIWindow.instance().frame(new Rect(0, 0, w, h));
		// 设置paint
		UITextButton.paintTextSize(h * 0.06f);
		UILabel.paintTextSize(h * 0.06f);
		
		UIView.sViews[UIView.VIEW_ROOT].frame(UIWindow.instance().frame()).visible(true);
		//
		UIView.sViews[UIView.VIEW_STATUS_PANEL].layout(w, h).visible(true);
		UIView.sViews[UIView.VIEW_DESKTOP_PANEL].layout(w, h).visible(true);
		UIView.sViews[UIView.VIEW_FLIP_PANEL].layout(w, h);
		UIView.sViews[UIView.VIEW_PLAY_PANEL].layout(w, h);
		UIView.sViews[UIView.VIEW_ONE_BUTTON_PANEL].layout(w, h).visible(true);
		UIView.sViews[UIView.VIEW_HANDY_CARD].layout(w, h).visible(true);
		// 最后才visible，避免前面的invalidate
		UIWindow.instance().visible(true);
	}

	@Override
	public void onTouchDown(int x, int y) {
		UIWindow.instance().onTouchDown(x, y);
	}
	//---------------GameViewDelegate-----------------------

	//---------------UIButton.OnTouchDownListener----------------------
	@Override
	public void onButtonTouchDown(UIButton button) {
		switch (button.tag()) {
		case UIView.VIEW_START_BUTTON:
			Game.instance().nextStage();
			button.visible(false);
			break;
		case UIView.VIEW_FLIP_SPADE_BUTTON:
		case UIView.VIEW_FLIP_HEART_BUTTON:
		case UIView.VIEW_FLIP_CLUB_BUTTON:
		case UIView.VIEW_FLIP_DIAMOND_BUTTON:
		case UIView.VIEW_FLIP_JOKER_BUTTON:
			GameData.instance().handPositionPlayer().flip(button.tag() - UIView.VIEW_FLIP_DIAMOND_BUTTON);
			break;
		case UIView.VIEW_PAUSE_BUTTON:
			Game.instance().pause();
			break;
		case UIView.VIEW_CONTINUE_BUTTON:
			Game.instance().recover();
			break;
		case UIView.VIEW_BURY_BUTTON:
		{
			UIHandyCardView view = UIView.sViews[UIView.VIEW_HANDY_CARD].toHandyCardView();
			GameData.instance().handPositionPlayer().doBuryCards(view.selectedCards());
			view.clearSelectedIndexes();
		}
			break;
		case UIView.VIEW_PREVIOUS_BUTTON:
		{
			if (GameData.instance().showingPrevious()) {
				mViewHandler.removeMessages(MESSAGE_CANCEL_SHOW_PREVIOUS);
				for (int i = 0; i < Player.PLAYER_COUNT; i++) {
					UIView.sViews[UIView.VIEW_BOTTOM_CARD + i].toCardView().cards(GameData.instance().playedCards().get(i));
				}
			} else {
				mViewHandler.sendEmptyMessageDelayed(MESSAGE_CANCEL_SHOW_PREVIOUS, INVERVAL_CANCEL);
				for (int i = 0; i < Player.PLAYER_COUNT; i++) {
					UIView.sViews[UIView.VIEW_BOTTOM_CARD + i].toCardView().cards(GameData.instance().lastRoundCards().get(i));
				}
			}
			GameData.instance().showingPrevious(!GameData.instance().showingPrevious());
		}
			break;
		case UIView.VIEW_PLAY_BUTTON:
		{
			UIHandyCardView view = UIView.sViews[UIView.VIEW_HANDY_CARD].toHandyCardView();
			GameData.instance().handPositionPlayer().doPlayCard(view.selectedCards());
			view.clearSelectedIndexes();
		}
			break;
		case UIView.VIEW_VIEW_KITTY_BUTTON:
		{
			boolean origin = UIView.sViews[UIView.VIEW_KITTY_CARD].visible();
			if (origin) {
				mViewHandler.removeMessages(MESSAGE_CANCEL_SHOW_KITTY);
			} else {
				mViewHandler.sendEmptyMessageDelayed(MESSAGE_CANCEL_SHOW_KITTY, INVERVAL_CANCEL);
			}
			UIView.sViews[UIView.VIEW_KITTY_CARD].visible(!origin);
		}
			break;
		case UIView.VIEW_TIP_BUTTON:
		{
			UIView.sViews[UIView.VIEW_HANDY_CARD].toHandyCardView().selectedCards(
					Strategy.instance().playCard(GameData.instance().handPositionPlayer(), 
							(PlayStage) Game.instance().currentStage()));
		}
			break;
		default:
			break;
		}
	}
	//---------------UIButton.OnTouchDownListener----------------------
	
	void initViews() {
		Resources res = UIWindow.instance().context().getResources();
		Bitmap[] cardBitmaps = new Bitmap[Card.COUNT_OF_ONE_DECK];
		int index = 0;
		for (int i = R.drawable.d01; i <= R.drawable.d13; i++, index++) {
			cardBitmaps[index] = BitmapFactory.decodeResource(res, i);
		}
		for (int i = R.drawable.c01; i <= R.drawable.c13; i++, index++) {
			cardBitmaps[index] = BitmapFactory.decodeResource(res, i);
		}
		for (int i = R.drawable.h01; i <= R.drawable.h13; i++, index++) {
			cardBitmaps[index] = BitmapFactory.decodeResource(res, i);
		}
		for (int i = R.drawable.s01; i <= R.drawable.s13; i++, index++) {
			cardBitmaps[index] = BitmapFactory.decodeResource(res, i);
		}
		cardBitmaps[index++] = BitmapFactory.decodeResource(res, R.drawable.j01);
		cardBitmaps[index++] = BitmapFactory.decodeResource(res, R.drawable.j02);
		Drawable enabledDrawable = new ColorDrawable(Color.CYAN);
		Drawable disabledDrawable = new ColorDrawable(Color.GRAY);
		Bitmap[] suitBitmaps = {BitmapFactory.decodeResource(res, R.drawable.diamond), 
				BitmapFactory.decodeResource(res, R.drawable.club),
				BitmapFactory.decodeResource(res, R.drawable.heart),
				BitmapFactory.decodeResource(res, R.drawable.spade),
				BitmapFactory.decodeResource(res, R.drawable.joker)};
		UIHelper.init(UIWindow.instance().context());
		
		// 根View
		UIView.sViews[UIView.VIEW_ROOT] = new UIView().addToView(UIWindow.instance());
		// 状态标签面板
		UIView.sViews[UIView.VIEW_STATUS_PANEL] = new UIStatusPanelView().
				addToView(UIView.sViews[UIView.VIEW_ROOT]);
		UIView.sViews[UIView.VIEW_MASTER_LABEL] = new UILabel().
				text(res.getString(R.string.master)).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_STATUS_PANEL]);
		UIView.sViews[UIView.VIEW_FLIPPER_LABEL] = new UILabel().
				text(res.getString(R.string.flipper)).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_STATUS_PANEL]);
		UIView.sViews[UIView.VIEW_HOUSE_LABEL] = new UILabel().
				text(res.getString(R.string.house)).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_STATUS_PANEL]);
		UIView.sViews[UIView.VIEW_SCORE_LABEL] = new UILabel().
				text(res.getString(R.string.score)).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_STATUS_PANEL]);
		UIView.sViews[UIView.VIEW_CURRENT_MASTER_LABEL] = new UILabel().
				text(res.getString(R.string.maxmaster)).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_STATUS_PANEL]);
		UIView.sViews[UIView.VIEW_CURRENT_FLIPPER_LABEL] = new UILabel().
				text(res.getString(R.string.none)).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_STATUS_PANEL]);
		UIView.sViews[UIView.VIEW_CURRENT_HOUSE_LABEL] = new UILabel().
				text(res.getString(R.string.none)).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_STATUS_PANEL]);
		UIView.sViews[UIView.VIEW_CURRENT_SCORE_LABEL] = new UILabel().
				text(res.getString(R.string.maxscore)).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_STATUS_PANEL]);
		UIView.sViews[UIView.VIEW_FLIPPED_SUIT] = new UIFlippedSuitView().
				suitBitmaps(suitBitmaps).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_STATUS_PANEL]);
		// 出牌面板
		UIView.sViews[UIView.VIEW_DESKTOP_PANEL] = new UIDesktopPanelView().
				addToView(UIView.sViews[UIView.VIEW_ROOT]);
		UIView.sViews[UIView.VIEW_TOP_CARD] = new UICardView().
				setCardBitmaps(cardBitmaps).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_DESKTOP_PANEL]);
		UIView.sViews[UIView.VIEW_LEFT_CARD] = new UICardView().
				setCardBitmaps(cardBitmaps).
				setAlign(UICardView.ALIGN_RIGHT).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_DESKTOP_PANEL]);
		UIView.sViews[UIView.VIEW_RIGHT_CARD] = new UICardView().
				setCardBitmaps(cardBitmaps).
				setAlign(UICardView.ALIGN_LEFT).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_DESKTOP_PANEL]);
		// 底牌
		UIView.sViews[UIView.VIEW_KITTY_CARD] = new UICardView().
				setCardBitmaps(cardBitmaps).
				addToView(UIView.sViews[UIView.VIEW_DESKTOP_PANEL]);
		UIView.sViews[UIView.VIEW_BOTTOM_CARD] = new UICardView().
				setCardBitmaps(cardBitmaps).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_DESKTOP_PANEL]);
		
		// 亮主面板
		UIView.sViews[UIView.VIEW_FLIP_PANEL] = new UIFlipPanelView().
				addToView(UIView.sViews[UIView.VIEW_ROOT]);
		UIView.sViews[UIView.VIEW_FLIP_SPADE_BUTTON] = new UIImageButton().
				image(suitBitmaps[Card.SUIT_SPADE]).
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				tag(UIView.VIEW_FLIP_SPADE_BUTTON).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_FLIP_PANEL]);
		UIView.sViews[UIView.VIEW_FLIP_HEART_BUTTON] = new UIImageButton().
				image(suitBitmaps[Card.SUIT_HEART]).
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				tag(UIView.VIEW_FLIP_HEART_BUTTON).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_FLIP_PANEL]);
		UIView.sViews[UIView.VIEW_FLIP_CLUB_BUTTON] = new UIImageButton().
				image(suitBitmaps[Card.SUIT_CLUB]).
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				tag(UIView.VIEW_FLIP_CLUB_BUTTON).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_FLIP_PANEL]);
		UIView.sViews[UIView.VIEW_FLIP_DIAMOND_BUTTON] = new UIImageButton().
				image(suitBitmaps[Card.SUIT_DIAMOND]).
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				tag(UIView.VIEW_FLIP_DIAMOND_BUTTON).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_FLIP_PANEL]);
		UIView.sViews[UIView.VIEW_FLIP_JOKER_BUTTON] = new UIImageButton().
				image(suitBitmaps[Card.SUIT_JOKER]).
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				tag(UIView.VIEW_FLIP_JOKER_BUTTON).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_FLIP_PANEL]);
		UIView.sViews[UIView.VIEW_PAUSE_BUTTON] = new UITextButton().
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				text(res.getString(R.string.pause)).
				tag(UIView.VIEW_PAUSE_BUTTON).
				visible(true).
				enabled(true).
				addToView(UIView.sViews[UIView.VIEW_FLIP_PANEL]);
		UIView.sViews[UIView.VIEW_CONTINUE_BUTTON] = new UITextButton().
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				text(res.getString(R.string.recover)).
				tag(UIView.VIEW_CONTINUE_BUTTON).
				enabled(true).
				addToView(UIView.sViews[UIView.VIEW_FLIP_PANEL]);
		// 剩余牌数
		UIView.sViews[UIView.VIEW_REMAINING_LABEL] = new UILabel().
				text(res.getString(R.string.remaincount)).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_FLIP_PANEL]);
		UIView.sViews[UIView.VIEW_CURRENT_REMAINING_LABEL] = new UILabel().
				text(res.getString(R.string.maxremaincount)).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_FLIP_PANEL]);
		// 出牌面板
		UIView.sViews[UIView.VIEW_PLAY_PANEL] = new UIPlayPanelView().
				addToView(UIView.sViews[UIView.VIEW_ROOT]);
		UIView.sViews[UIView.VIEW_PREVIOUS_BUTTON] = new UITextButton().
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				text(res.getString(R.string.previous)).
				tag(UIView.VIEW_PREVIOUS_BUTTON).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_PLAY_PANEL]);
		UIView.sViews[UIView.VIEW_TIP_BUTTON] = new UITextButton().
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				enabled(true).
				text(res.getString(R.string.tip)).
				tag(UIView.VIEW_TIP_BUTTON).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_PLAY_PANEL]);
		UIView.sViews[UIView.VIEW_PLAY_BUTTON] = new UITextButton().
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				text(res.getString(R.string.playcard)).
				tag(UIView.VIEW_PLAY_BUTTON).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_PLAY_PANEL]);
		UIView.sViews[UIView.VIEW_VIEW_KITTY_BUTTON] = new UITextButton().
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				text(res.getString(R.string.viewkitty)).
				tag(UIView.VIEW_VIEW_KITTY_BUTTON).
				visible(true).
				addToView(UIView.sViews[UIView.VIEW_PLAY_PANEL]);
		// 单按钮面板
		UIView.sViews[UIView.VIEW_ONE_BUTTON_PANEL] = new UISingleButtonPanelView().
				addToView(UIView.sViews[UIView.VIEW_ROOT]);
		// 开始按钮
		UIView.sViews[UIView.VIEW_START_BUTTON] = new UITextButton().
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				enabled(true).
				text(res.getString(R.string.start)).
				tag(UIView.VIEW_START_BUTTON).
				addToView(UIView.sViews[UIView.VIEW_ONE_BUTTON_PANEL]);
		// 扣底按钮
		UIView.sViews[UIView.VIEW_BURY_BUTTON] = new UITextButton().
				enabledBackgroundDrawable(enabledDrawable).
				disabledBackgroundDrawable(disabledDrawable).
				listener(this).
				enabled(true).
				text(res.getString(R.string.bury)).
				tag(UIView.VIEW_BURY_BUTTON).
				addToView(UIView.sViews[UIView.VIEW_ONE_BUTTON_PANEL]);
		// 手牌
		UIView.sViews[UIView.VIEW_HANDY_CARD] = new UIHandyCardView().
				setCardBitmaps(cardBitmaps).
				addToView(UIView.sViews[UIView.VIEW_ROOT]);
	}
}
